package com.jcgyl.export.util;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.jcgyl.export.dao.TbbcusunitDao;
import com.jcgyl.export.dao.TbciqmoneyDao;
import com.jcgyl.export.dao.TbciqpckagetypeDao;
import com.jcgyl.export.dao.TbcuscountryDao;
import com.jcgyl.export.dao.TbdictionaryDao;
import com.jcgyl.export.dao.TbedpinfoDao;
import com.jcgyl.export.dao.TblogisticDao;
import com.jcgyl.export.entity.Tbbcusunit;
import com.jcgyl.export.entity.Tbciqmoney;
import com.jcgyl.export.entity.Tbciqpckagetype;
import com.jcgyl.export.entity.Tbcuscountry;
import com.jcgyl.export.entity.Tbdictionary;
import com.jcgyl.export.entity.Tbedpinfo;
import com.jcgyl.export.entity.Tblogisticinfo;

@Component
public class DictionaryHelper {
	@Autowired
	private static StringRedisTemplate stringRedisTemplate;

	@Autowired
	private static RedisTemplate redisTemplate;
	@Autowired
	private static TbdictionaryDao tbdictionaryDao;
	@Autowired
	private static TbcuscountryDao tbcuscountryDao;
	@Autowired
	private static TbedpinfoDao tbedpinfoDao;
	@Autowired
	private static TblogisticDao tblogisticDao;
	@Autowired
	private static TbciqmoneyDao tbciqmoneyDao;
	@Autowired
	private static TbbcusunitDao tbbcusunitDao;
	@Autowired
	private static TbciqpckagetypeDao tbciqpckagetypeDao;

	@Autowired
	public void settbdictionaryDao(TbdictionaryDao _bdictionaryDao) {
		tbdictionaryDao = _bdictionaryDao;
	}

	@Autowired
	public void settbcuscountryDao(TbcuscountryDao _bcuscountryDao) {
		tbcuscountryDao = _bcuscountryDao;
	}

	@Autowired
	public void settbedpinfoDao(TbedpinfoDao _bedpinfoDao) {
		tbedpinfoDao = _bedpinfoDao;
	}

	@Autowired
	public void settblogisticDao(TblogisticDao _tblogisticDao) {
		tblogisticDao = _tblogisticDao;
	}

	@Autowired
	public void settbciqmoneyDao(TbciqmoneyDao _tbciqmoneyDao) {
		tbciqmoneyDao = _tbciqmoneyDao;
	}

	@Autowired
	public void settbbcusunitDao(TbbcusunitDao _tbbcusunitDao) {
		tbbcusunitDao = _tbbcusunitDao;
	}

	@Autowired
	public void settbciqpckagetypeDao(TbciqpckagetypeDao _tbciqpckagetypeDao) {
		tbciqpckagetypeDao = _tbciqpckagetypeDao;
	}

	private static List<Tbdictionary> _dict_data = null;
	private static List<Tbcuscountry> _dict_country = null;
	private static List<Tbedpinfo> _dict_edpinfo = null;
	private static List<Tblogisticinfo> _dict_logistic = null;
	private static List<Tbciqmoney> _dict_money = null;
	private static List<Tbciqpckagetype> _dict_ciq_package = null;
	private static List<Tbbcusunit> _dict_cus_unit = null;

	public static void Init() {

		_dict_data = tbdictionaryDao.findAllDictionaries();
		_dict_country = tbcuscountryDao.findAllCountries();
		_dict_edpinfo = tbedpinfoDao.findAllEdpinfos();
		_dict_logistic = tblogisticDao.findAllLogisticinfos();
		_dict_money = tbciqmoneyDao.findAllMoney();
		_dict_ciq_package = tbciqpckagetypeDao.findAllTypes();
		_dict_cus_unit = tbbcusunitDao.findAllUnits();
	}

	public static Tbdictionary GetValueById(int dict_id) {
		Tbdictionary dict = _dict_data.stream().filter((mapper) -> (mapper.getDictionarieid().equals(dict_id)))
				.findFirst().orElse(null);
		return dict;
	}

	public static Tbdictionary GetDefaultValue(String dict_type) {
		Tbdictionary dict = _dict_data.stream()
				.filter(data -> (data.getTypeid().equals(dict_type) && data.getIsdefault().equals(1))).findFirst()
				.orElse(null);
		return dict;
	}

	public static Tbdictionary GetDictionaryByName(String dict_type, String dict_name) {
		Tbdictionary dict = _dict_data.stream()
				.filter(data -> (data.getTypeid().equals(dict_type) && data.getDictionariename().equals(dict_name)))
				.findFirst().orElse(null);
		return dict;
	}

	public static List<Tbdictionary> GetDictionariesByType(String dict_type) {
		List<Tbdictionary> dict = _dict_data.stream().filter(data -> (data.getTypeid().equals(dict_type)))
				.collect(Collectors.toList());
		return dict;
	}

	/// <summary>
	/// 根据缩写获取国家信息
	/// </summary>
	/// <param name="abbre"></param>
	/// <returns></returns>
	public static Tbcuscountry GetCountryByAbbre(String abbre) {
		Tbcuscountry country = _dict_country.stream().filter(m -> m.getCountryabbreviation().equals(abbre)).findFirst()
				.orElse(null);
		return country;
	}

	public static Tbcuscountry GetCountryByCustomsCode(String code) {
		Tbcuscountry country = _dict_country.stream().filter(m -> m.getCustomscode().equals(code)).findFirst()
				.orElse(null);
		return country;
	}

	public static Tbcuscountry GetCountryByCiqCode(String code) {
		Tbcuscountry country = _dict_country.stream().filter(m -> m.getCountrycode().equals(code)).findFirst()
				.orElse(null);
		return country;
	}

	public static Tbcuscountry GetCountryByChineseName(String name) {
		Tbcuscountry country = _dict_country.stream().filter(m -> m.getCountryname().equals(name)).findFirst()
				.orElse(null);
		return country;
	}

	/// <summary>
	/// 获取电商信息或者电商平台信息
	/// </summary>
	/// <param name="type"></param>
	/// <returns></returns>
	public static List<Tbedpinfo> GetEdpInfo(int type) {
		List<Tbedpinfo> edp_info = _dict_edpinfo.stream().filter(m -> m.getEdpType().equals(type))
				.collect(Collectors.toList());
		return edp_info;
	}

	public static Tbciqpckagetype GetCiqPackageType(String name) {
		Tbciqpckagetype package_type = _dict_ciq_package.stream().filter(m -> m.getPackCname().equals(name)).findFirst()
				.orElse(null);
		return package_type;
	}

	public static List<Tblogisticinfo> GetLogisticInfos() {
		return _dict_logistic;
	}

	public static Tbciqmoney GetCoinByAbbreviation(String abbre) {
		Tbciqmoney coin = _dict_money.stream().filter(m -> m.getMoneyabbreviation().equals(abbre)).findFirst()
				.orElse(null);
		return coin;
	}

	public static Tbciqmoney GetCoinByCustomCode(String abbre) {
		Tbciqmoney coin = _dict_money.stream().filter(m -> m.getCustomscode().equals(abbre)).findFirst().orElse(null);
		return coin;
	}

	public static Tbbcusunit GetCusUnitByCode(String code) {
		Tbbcusunit unit = _dict_cus_unit.stream().filter(m -> m.getCode().equals(code)).findFirst().orElse(null);
		return unit;
	}

	public static List<Tbcuscountry> getCUSCountry() {
		return _dict_country;
	}

}